﻿using MotorLibrary.Model;
using System;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Data.SqlClient;

namespace MotorLibrary.DAL
{
    public class MotorDbContext: DbContext
    {
        public MotorDbContext(IDatabaseInitializer<MotorDbContext> initializer): base("MotorDb")
        {
            Database.SetInitializer<MotorDbContext>(initializer);
            Database.Initialize(true);
        }

        public MotorDbContext(IDatabaseInitializer<MotorDbContext> initializer, bool contextOwnsConnection): base(Connection, contextOwnsConnection)
        {
            Database.SetInitializer<MotorDbContext>(initializer);
            Database.Initialize(true);
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
        }

        [ThreadStatic]
        public static SqlConnection Connection;

        public DbSet<Tipo> Tipos { get; set; }
        public DbSet<Endpoint> Endpoints { get; set; }
        public DbSet<Ejecucion> Ejecuciones { get; set; }
        public DbSet<Variable> Variables { get; set; }
        public DbSet<VariableEjecucion> VariableEjecuciones { get; set; }
    }
}
